package org.jeecgframework.core.util.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import jodd.util.StringUtil;


/**
 * 共通方法类
 * 
 * @author BCH
 *
 */
public class CommonFunc {
	
	
	/** 
	 * 解开GZIP压缩的网页内容
	*/
	public static String uncompress(InputStream in, String charset) {
		try {
			
			GZIPInputStream gInputStream = new GZIPInputStream(in);
			InputStreamReader isr = new InputStreamReader(gInputStream , charset);
			BufferedReader br = new java.io.BufferedReader(isr);
			StringBuffer strBuffer = new StringBuffer();
			String tempbf;
            while ((tempbf = br.readLine()) != null) {
            	strBuffer.append(tempbf).append("\n");
            }
//			while ((len = gInputStream.read(by)) != -1){
//				strBuffer.append(new String(by, 0, len, charset));
//			}
			return strBuffer.toString();
		} catch (Exception e) {
//			e.printStackTrace();
			String backStr = "";
			try {
//				in.mark(0);
				BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
				
				String line = null;
				while ((line = reader.readLine()) != null) {
//					System.out.println(line);
					if(StringUtil.isEmpty(backStr)){	//第一行处理
						if(line.startsWith("DOCTYPE html PUBLIC"))
							line = "<!"+line;
						if(line.toLowerCase().startsWith("eta"))
							line = "<m"+line;
					}
					
					
					backStr += line;
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
			return backStr;
		}finally{
			if(in != null)
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
	
	/** 
	 * 解开GZIP压缩的网页内容 [JSON 格式]
	*/
	public static String uncompressjson(InputStream in, String charset) {
		try {
			GZIPInputStream gInputStream = new GZIPInputStream(in);
			byte[] by = new byte[8192];
			StringBuffer strBuffer = new StringBuffer();
			int len = 0;
			while ((len = gInputStream.read(by)) != -1) {
				strBuffer.append(new String(by, 0, len, charset));
			}
			return strBuffer.toString();
		} catch (Exception e) {
			String backStr = "{"; // 对于JSON格式的数据，如果不是GZIP 则主动添加json头
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
				String line = null;
				while ((line = reader.readLine()) != null) {
					backStr += line;
				}
			} catch (Exception e1) {
//				e1.printStackTrace();
			}
			return backStr;
		}finally{
			if(in != null)
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
	
	public static String uncompressjson_bak(InputStream in, String charset) {
		try {
			GZIPInputStream gInputStream = new GZIPInputStream(in);
			byte[] by = new byte[8192];
			StringBuffer strBuffer = new StringBuffer();
			int len = 0;
			while ((len = gInputStream.read(by)) != -1) {
				strBuffer.append(new String(by, 0, len, charset));
			}
			return strBuffer.toString();
		} catch (Exception e) {
			String backStr = "{"; // 对于JSON格式的数据，如果不是GZIP 则主动添加json头
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
				String line = null;
				while ((line = reader.readLine()) != null) {
					backStr += line;
				}
			} catch (Exception e1) {
//				e1.printStackTrace();
			}
			
			return backStr;
		}
	}
	
	
}
 